//
//  CSSearchViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/3/29.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "CSSearchViewController.h"
#import "GoodListModel.h"
//#import "CSgoodsViewController.h"
#import "HomeGoodsCell.h"
//#import "CSsearchModel.h"
#import "DISgoodsDetailViewController.h"

@interface CSSearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *modelArr;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) UITextField *mytf;

@end

@implementation CSSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    UITextField *search = [[UITextField alloc]init];
    [search createLeftViewImage:@"saosuo"];
    search.delegate = self;
    self.navigationItem.titleView = search;
    search.keyboardType = UIKeyboardTypeWebSearch;
    search.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    search.text = _search;
    _mytf =search;
    self.page = 1;
    
    [self.rightbutton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.rightbutton setImage:imgname(@"sousuobgimage") forState:(UIControlStateNormal)];
    self.modelArr = [NSMutableArray arrayWithCapacity:1];
    [self.view addSubview:self.collectionView];
    self.rightbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.collectionView reloadData];
    [self requestGoods];
    // Do any additional setup after loading the view.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [super textFieldShouldReturn:textField];
    _search = textField.text;
    _page = 1;
    [self requestGoods];
    return YES;
}

- (void)rightClick{
    _search = _mytf.text;
    _page = 1;
    [self requestGoods];
}

- (void)requestGoods {
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"name"] = _search;
    NSString *pp = [NSString stringWithFormat:@"%ld",(long)_page];
    param[@"pageNum"] = pp;
    if (_page == 1) {
        [self.modelArr removeAllObjects];
    }
    [NetWorkTool POST:getPlatProList param:param success:^(id dic) {
        NSArray *myarr = [dic objectForKey:@"data"];
        for (NSDictionary *mydic in myarr) {
            GoodListModel *model = [[GoodListModel alloc]init];
            [model setValuesForKeysWithDictionary:mydic];
            [self.modelArr addObject:model];
        }
        [self.collectionView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

#pragma mark - CollectionView -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodsCell" forIndexPath:indexPath];
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    [cell refreshs:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath {
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    DISgoodsDetailViewController *vc = [DISgoodsDetailViewController new];
    vc.goodsID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(KWIDTH/2 , 260);
        //        layout.headerReferenceSize = CGSizeMake(KWIDTH, 204);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) collectionViewLayout:layout];
        adjustInset(_collectionView);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor =RGBA(237, 237, 237, 1);
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"HomeGoodsCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self requestGoods];
        }];
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page += 1;
            [self requestGoods];
        }];
        [_collectionView reloadData];
    }
    return _collectionView;
}

@end

